package io.coderyeah.ymcc.quartz;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lqs
 * @date 2022/11/6 11:07
 */
@Configuration
public class QuartzConfig {
    @Value("${sue.spring.quartz.cron}")
    private String cron;

    /**
     * 创建定时任务
     */
    @Bean
    public JobDetail quartzCartJobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(QuartzCartJob.class)
                .withIdentity("quartzCartJobDetail", "QUARTZ_CART")
                .usingJobData("userName", "coderyeah")
                .storeDurably()
                .build();
        return jobDetail;
    }

    /**
     * 创建触发器
     */
    @Bean
    public Trigger quartzCartJobTrigger() {
        //每隔5秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(quartzCartJobDetail())
                .withIdentity("quartzCartJobTrigger", "QUARTZ_CART_JOB_TRIGGER")
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }
}

